package com.jws.jawestshow;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.jws.JwsIntents;
import com.jws.thirdlibrary.CxManager;


public class InfraredActivity extends Activity {
    private Button mButtonOpen;
    private Button mButtonClose;
    private TextView mInfraredstate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_infrared);
        mInfraredstate = (TextView) findViewById(R.id.infraredstate);
        initButton();
        initDate();
    }

    CxManager mCxManager;

    private void initDate() {
        mCxManager = JawestshowApplication.getCxManager();
    }

    private void initButton() {
        mButtonOpen = (Button) findViewById(R.id.open);
        mButtonClose = (Button) findViewById(R.id.close);
        mButtonOpen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterInfraredReceiver();
                Toast.makeText(InfraredActivity.this, R.string.jws_open_success, Toast.LENGTH_SHORT).show();
            }
        });

        mButtonClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMyInfraredReceiver != null) {
                    mCxManager.jwsUnregisterIRListener();
                    unregisterReceiver(mMyInfraredReceiver);
                    mMyInfraredReceiver = null;
                    mInfraredstate.setText("");
                    Toast.makeText(InfraredActivity.this, R.string.jws_close_success, Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    public void RegisterInfraredReceiver() {
        mMyInfraredReceiver = new MyInfraredReceiver();
        IntentFilter intentFilter = new IntentFilter(JwsIntents.REQUEST_RESPONSE_IR_STATE_ACTION);
        registerReceiver(mMyInfraredReceiver, intentFilter);
        mCxManager.jwsRegisterIRListener();
    }

    Object object;
    MyInfraredReceiver mMyInfraredReceiver;

    class MyInfraredReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean state = intent.getBooleanExtra(JwsIntents.EXTRA_IR_STATE_RESPONSE, false);
            Log.i("byf-infraed", "onReceive() " + "state :" + state);
            if (state) {
                String Strstate = getString(R.string.jws_Infrared_true);
                mInfraredstate.setText(Strstate);
            } else {
                String Strstate = getString(R.string.jws_Infrared_false);
                mInfraredstate.setText(Strstate);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMyInfraredReceiver != null) {
            unregisterReceiver(mMyInfraredReceiver);
        }
    }
}
